<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Mcm\RepasBundle\Form;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;
/**
 * Description of CoderepasHandler
 *
 * @author Gael
 */
class CoderepasHandler {
    protected $form;
    protected $request;
    protected $em;
    protected $user;
   public function __construct(Form $form, Request $request, EntityManager $em, \Mcm\UserBundle\Entity\User $user)
    {
        $this->form    = $form;
        $this->request = $request;
        $this->em      = $em;
        $this->user = $user;
    }
     public function process()
    {
        if( $this->request->getMethod() == 'POST' )
        {
             
            $this->form->bindRequest($this->request);
                $data= $this->request;
                
                 return $this->onSuccess($data);
            
        }
         
        return false;
    }
    public function onSuccess($data)
    {
        
        $paiement =$this->em->getRepository('McmRepasBundle:Paiement')->EatCodeRepas($this->form->get('code')->getData());
        if($paiement){
            $paiement->setEtat('valide');
            $this->user->addAcompte($paiement->getRepas()->getPrix());
            $this->em->merge($paiement);
            $this->em->merge($this->user);
            $this->em->flush();
            return true;
        }else{
            return false;
        }
        
    }
}

?>
